package com.software.modelcenter.controller;

import com.software.modelcenter.entity.Collection;
import com.software.modelcenter.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @RequestMapping(value = "/collectModel")
    public Collection collectModel(@RequestBody Collection collection){
        //得先判断存不存在
        String modelId=collection.getModelId();
        String userId = collection.getUserId();
        Collection collection1 = collectionService.findCollection(userId, modelId);
        if(Objects.isNull(collection1)){
            Collection save = collectionService.creatCollection(collection);
            return save;
        }else
            return null;


    }

    @GetMapping("/findCollection")
    public Collection findCollection(@RequestParam(name = "userId") String userId,
                                   @RequestParam(name = "modelId") String modelId){

        Collection find = collectionService.findCollection(userId, modelId);
        return find;
    }
    @RequestMapping(value = "/deleteCollectModel")
    public void deleteCollectModel(@RequestBody Collection collection){
        String userId=collection.getUserId();
        String modelId=collection.getModelId();
        Collection find = findCollection(userId, modelId);
        collectionService.deleteCollection(find);
    }

    @RequestMapping(value = "/isCollected")
    public Collection isCollected(@RequestBody Collection collection){
        String modelId=collection.getModelId();
        String userId = collection.getUserId();
        Collection collection1 = collectionService.findCollection(userId, modelId);
        return collection1;
    }

}
